package com.jinxin.platform.cdsmsmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsmsmgr.mapper.SmsSendRecordMapper;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SmsSendRecord;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SysUserVO;
import com.jinxin.platform.cdsmsmgr.pojo.entity.CommonCodeEntity;
import com.jinxin.platform.cdsmsmgr.pojo.entity.Constant;
import com.jinxin.platform.cdsmsmgr.pojo.entity.ResultEntity;
import com.jinxin.platform.cdsmsmgr.rpc.SysUserRpc;
import com.jinxin.platform.cdsmsmgr.service.ISendRecordService;
import com.jinxin.platform.cdsmsmgr.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-07-03
 * @Author: Lingnan
 **/
@Service
public class SmsSendRecordServiceImpl implements ISendRecordService {
    @Autowired
    private SmsSendRecordMapper sendRecordMapper;
    @Autowired
    private SysUserRpc sysUserRpc;

    @Override
    public List<SmsSendRecord> listPage(HashMap param) {
        return sendRecordMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return sendRecordMapper.listPageCount(param);
    }

    @Override
    public ResultEntity saveOrUpdate(SmsSendRecord sendRecord) {
        int count;
        if(!StringUtils.isEmpty(sendRecord.getId())){
            count = sendRecordMapper.updateByKey(sendRecord);
        }else {
            sendRecord.setId(Utils.getMillPrimaryKey());
            sendRecord.setSendTime(LocalDateTime.now());
            sendRecord.setOperateUser(Constant.ACCOUNT_ADMIN);
            count = sendRecordMapper.insert(sendRecord);
        }
        if(count > 0){
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc",sendRecord);
        }else {
            return new ResultEntity(CommonCodeEntity.FAIL,"保存失败","");
        }
    }

    @Override
    public ResultEntity deleteById(String id) {
        if(StringUtils.isEmpty(id)){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"id为空","");
        }
        int count = sendRecordMapper.deleteByKey(id);
        if(count > 0){
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc","");
        }
        return new ResultEntity(CommonCodeEntity.FAIL,"删除失败","");
    }

    @Override
    public int batchInsert(List<SmsSendRecord> sendRecords) {
        return sendRecordMapper.batchInsert(sendRecords);
    }

    @Override
    public JsonResult batchDelete(String[] idArr) {
        List<String> idList = Arrays.stream(idArr).collect(Collectors.toList());
        sendRecordMapper.deleteByIds(idList);
        return JsonResult.ok("删除成功",null);
    }
}
